package com.controller;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.github.yulichang.wrapper.UpdateJoinWrapper;
import com.pojo.DataBase;
import com.pojo.Role;
import com.pojo.User;
import com.service.UserService;
import com.vo.UserDTO;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.authz.AuthorizationException;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.sql.Wrapper;
import java.util.List;

//@RestController
@Controller
public class UserController {
    @Resource
    private UserService userService;

    @Resource
    private DataBase dataBase;

    /*@RequiresPermissions("user:add")
    @RequiresRoles("admin1")*/
    /*@PostMapping("/getUserList")
    @ResponseBody
    public List<UserDTO> getUserList(){
        *//*System.out.println(dataBase);
        List<User> userList = userService.list();
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("name","李");*//*

        *//*Page<User> page = new Page<>();
        page.setCurrent(1);
        page.setSize(10);

        Page<User> page1 = userService.page(page);
        List<User> userList2 = page1.getRecords();*//*

        *//*PageHelper.startPage(1,10);
        List<User> userList1 = userService.list(queryWrapper);*//*

//        EasyExcel.write("D:\\user.xlsx",User.class).sheet("名单").doWrite(userList);

        MPJLambdaWrapper<User> wrapper = new MPJLambdaWrapper<User>();
        wrapper.selectAll(User.class);
        wrapper.select(Role::getrId);
        wrapper.select(Role::getGrade);
        wrapper.leftJoin(Role.class,Role::getrSex,User::getSex);
        wrapper.eq("name","李四");

        List<UserDTO> userList = userService.selectJoinList(UserDTO.class,wrapper);

        return userList;
    }*/
    @RequestMapping("/getUserList")
    @ResponseBody
    public List<User> getUserList(){
        List<User> list = userService.list();
        return list;
    }

    @GetMapping("/addUser")
    public String addUser(){
        String path = "D:\\user.xlsx";
//        boolean b = userService.ExcelImport(path);
        String result = "fail";
        if (true){
            result = "success";
        }
        return result;
    }


    @GetMapping("/index1")
    public String index1(Model model){
        model.addAttribute("msg","ssssss");
        List<User> userList = userService.list();
        model.addAttribute("userList",userList);
        return "index1";
    }


    @PostMapping("/getUserList1")
    @ResponseBody
    public List<User> getUserList1(@RequestBody User user){
        System.out.println(user.getId());
        List<User> userList = userService.list();

        return userList;
    }

    @PostMapping("/doLogin")
    public String doLogin(String username, String password, Model model){
        Subject subject = SecurityUtils.getSubject();
        UsernamePasswordToken token = new UsernamePasswordToken(username, password);
        try {
            //进行验证，这里可以捕获异常，然后返回对应信息
            subject.login ( token );
        } catch ( UnknownAccountException e ) {
            model.addAttribute ( "msg", "用户不存在" );
            return "login";
        } catch ( AuthenticationException e ) {
            model.addAttribute ( "msg", "密码错误" );
            return "login";
        } catch ( AuthorizationException e ) {
            model.addAttribute ( "msg", "认证失败" );
            return "login";        }
        return "index";
    }

    //共享session
    @PostMapping("/sessionLogin")
    @ResponseBody
        public String sessionLogin(HttpSession session){
        User user = userService.getById(1);
        session.setAttribute ("user",user);
        return "登陆成功";
    }

    //获取共享session
    @PostMapping("/sessionGet")
    @ResponseBody
    public User sessionGet(HttpSession session){
        User user = (User)session.getAttribute("user");
        return user;
    }

    @PostMapping("/signIn")
    public User SignIn(@RequestBody User user1){
        User user = userService.SignIn(user1.getId(), user1.getPassword());
        return user;
    }

    @RequestMapping("/addUser")
    @ResponseBody
    public String addUser(@RequestBody User user){
        boolean save = userService.save(user);
        return "添加成功";
    }

    @RequestMapping("/delUser")
    @ResponseBody
    public String delUser(@RequestBody int id){
        boolean b = userService.removeById(id);
        return "删除成功";
    }

    @RequestMapping("/updateUser")
    @ResponseBody
    public String updateUser(@RequestBody User user){
        UpdateWrapper<User> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("id", user.getId());
        boolean b = userService.update(user,updateWrapper);
        return "修改成功";
    }

}